package com.ruoyi.web.core.interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.ruoyi.common.base.Result;
import com.ruoyi.jl.domain.Player;
import com.ruoyi.jl.mapper.PlayerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * 登陆检查，
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Autowired
    PlayerMapper playerMapper;

    //目标方法执行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 判断token,验证是否登录成功
        String servletPath = request.getServletPath();
        String[] arr = {"/ql/login", "/ql/goldCount"};
        for(String str : arr) {
            if(servletPath.equals(str)) {
                return true;
            }
        }
        String str = ReadAsChars(request);
        JSONObject jsonObject = JSONObject.parseObject(str);
        String token = jsonObject.getString("token");
        String playerId = jsonObject.getString("playerId");
        if (Strings.isNullOrEmpty(token) || Strings.isNullOrEmpty(playerId)) {
            response.setContentType("application/json; charset=utf-8");
            response.getWriter().print(JSON.toJSONString(Result.failed(401, "playerId or token缺失!")));
            return false;
        }
        if(token.equals("qunling123")) {
            return true;
        }
        Player player = playerMapper.selectById(playerId);
        String storeToken = player.getToken();
        if (token.equals(storeToken)) {
            return true;
        } else {
            response.setContentType("application/json; charset=utf-8");
            response.getWriter().print(JSON.toJSONString(Result.failed(400, "token失效!")));
            return false;
        }
    }

    public static String ReadAsChars(HttpServletRequest request) {

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder("");
        try {
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
